<?php

require '../utils/conn.php';
$fileName = $_FILES["file"]["name"];
$image;
if(!empty($fileName)) {

	$tmpName = $_FILES["file"]["tmp_name"];

	$error = $_FILES["file"]["error"];
	if($error > 0) { // 上传失败
		$msg = json_encode("图片上传发生错误");
		echo '{"code":"1","msg":'.$msg.'}';
		echo("<script language=javascript>alert('$msg'); history.go(-1)</script>");
		exit();
	}

	$fileSplit = explode(".", $fileName);
	$nSize = count($fileSplit);
	if(2 > $nSize) { // 没有文件后缀名?
		$msg = json_encode("上传的文件'$fileName'不合法");
		echo '{"code":"1","msg":'.$msg.'}';
		echo("<script language=javascript>alert('$msg'); history.go(-1)</script>");
		exit();
	}
	$suffix = $fileSplit[$nSize-1];

	$size = $_FILES["file"]["size"];
	if($size > 2097152) { // 超过2M
		$msg = json_encode("上传的图片超过2M");
		echo '{"code":"1","msg":'.$msg.'}';


		echo("<script language=javascript>alert('$msg'); history.go(-1)</script>");
		exit();
	}

	$image = "ad_".time().".".$suffix;
	move_uploaded_file($tmpName, "pic/$image");
}
$title=$_REQUEST['title'];
$subTitle=$_REQUEST['subtitle'];
$linkurl=$_REQUEST['linkurl'];
if (empty($image)) {

	$query = "update advertising_image set title='$title', subtitle='$subTitle',link_url='$linkurl'";
	$result = mysql_query($query);
}
else {

	$query = "update advertising_image set title='$title', subtitle='$subTitle',link_url='$linkurl', image_url='$image'";
	$result = mysql_query($query);
}

echo("<script language=javascript>alert('广告更新成功！'); history.go(-1)</script>");

?>